package student.aistar.seckillOrder;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;
import student.aistar.model.entity.Orders;
import student.aistar.service.ITVSeckillService;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * 本类用来演示:  去除消息队列的订单
 *
 * @author: qinFeng
 * @date: 2021/10/13 17:58
 */
@Component
public class seckillOrder {
    @Autowired
    private ITVSeckillService seckillService;

    @RabbitListener(queues = "spikeOrderQueue")
    @RabbitHandler
    public void generateOrder( Map<String, Integer> map, @Headers Map<String,Object> header, Channel channel){
        Integer userId = map.get("userId");
        Integer killId = map.get("killId");

//        session.getAttribute("user")
        String orderNumber = new Date().getTime() + "" + userId;
        Orders orders = new Orders();
        orders.setUserId(userId);
        orders.setOrderNumber(Long.valueOf(orderNumber));
        seckillService.insertOrders(orders);
        seckillService.insertOrderDetail(orders.getOrderId());
        //手动删除rabbitmq队列中的数据的.
        long ack = (long) header.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
